﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace SmartApi.Utils
{
    public class UrlPathParser
    {
        private readonly Regex _reqParamsTokens;
        public UrlPathParser()
        {
            var regOptions = RegexOptions.Multiline | RegexOptions.CultureInvariant | RegexOptions.Compiled;
            _reqParamsTokens = new Regex(@"\{([\p{L}\p{N}_]+)\}", regOptions);
        }
        public string Parse(string urlPath, IDictionary<string, RequestParameter> parameters)
        {
            return _reqParamsTokens.Replace(urlPath, (match) =>
            {
                string paramName = match.Groups[1].Value;
                if (parameters.ContainsKey(paramName))
                {
                    var reqParam = parameters[paramName];
                    if (reqParam == null || reqParam.Value == null) { return ""; }
                    return reqParam.Value.ToString();
                }
                return match.Value;
            });
        }
    }
}
